package com.xiaobai.netease_news.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.model.Clist;
import com.xiaobai.netease_news.model.Radio;
import com.xiaobai.netease_news.model.Tlist;
import com.xiaobai.netease_news.utils.RequestManager;
import com.xiaobai.netease_news.utils.ViewHolder;

import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

public class PlayerItemActivity extends Activity implements AdapterView.OnItemClickListener {

    private String cid;
    private List<Tlist> tlistList;
    private MyAdapter adapter;
    private PullToRefreshListView ptrlistView;
    private ListView listView;
    private TextView txtTitle;
    private String cname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_item);
        Intent intent = getIntent();
        cid = intent.getStringExtra("cid");
        cname = intent.getStringExtra("cname");
        Log.d("cidintent", "cidintent:" + cid);
        tlistList = new LinkedList<>();
        adapter = new MyAdapter(tlistList);
        txtTitle = (TextView) findViewById(R.id.playeritem_title);
        txtTitle.setText(cname);
        ptrlistView = (PullToRefreshListView) findViewById(R.id.playeritem_listView);
        listView = ptrlistView.getRefreshableView();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String url = "http://c.m.163.com/nc/topicset/android/radio/" + cid + "/0-20.html";
        RequestQueue requestQueue = RequestManager.getInstance(this).getRequestQueue();
        JsonObjectRequest request = new JsonObjectRequest(
                url,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Clist clist = new Clist();
                        clist.parseJSON(jsonObject);
                        List<Tlist> tlistLists = clist.getTlistList();
                        tlistList.addAll(0, tlistLists);
                        Log.d("tlistList", "tlistList:" + PlayerItemActivity.this.tlistList);
                        adapter.notifyDataSetChanged();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        volleyError.printStackTrace();
                    }
                }
        );
        requestQueue.add(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String tid = tlistList.get(position).getTid();
        Intent intent = new Intent();
        intent.putExtra("tid", tid);
        intent.setClass(PlayerItemActivity.this, PlayerActivity.class);
        startActivity(intent);
    }

    private class MyAdapter extends BaseAdapter {

        private List<Tlist> tlistList;

        public MyAdapter(List<Tlist> tlistList) {
            this.tlistList = tlistList;
        }

        @Override
        public int getCount() {
            int ret = 0;
            if (tlistList != null) {
                ret = tlistList.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            Object ret = null;
            if (tlistList != null) {
                ret = tlistList.get(position);
            }
            return ret;
        }

        @Override
        public long getItemId(int position) {
            long ret = 0;
            if (tlistList != null) {
                ret = position;
            }
            return ret;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View ret;
            if (convertView != null) {
                ret = convertView;
            } else {
                ret = LayoutInflater.from(PlayerItemActivity.this).inflate(R.layout.playeritem_adapter_item, parent, false);
            }
            ImageView imageHead = ViewHolder.getView(ret, R.id.playeritem_item_image);
            TextView txtTitle = ViewHolder.getView(ret, R.id.playeritem_item_title);
            TextView txtContent = ViewHolder.getView(ret, R.id.playeritem_item_content);
            TextView txtCount = ViewHolder.getView(ret, R.id.playeritem_item_count);
            Radio radio = tlistList.get(position).getRadio();
            String imgsrc = radio.getImgsrc();
            ImageLoader imageLoader = RequestManager.getInstance(PlayerItemActivity.this).getImageLoader();
            imageLoader.get(
                    imgsrc,
                    imageLoader.getImageListener(imageHead, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
            );
            txtTitle.setText(tlistList.get(position).getTname());
            txtContent.setText(tlistList.get(position).getRadio().getTitle());
            txtCount.setText(String.valueOf(tlistList.get(position).getRadio().getReplyCount()));
            return ret;
        }
    }
}
